#include "BondMarketStreamV2.h"
#include "RealTimeDealUtil.h"

void CMarketStream::RefreshKeyToContainer(CSSVContainer* pssv, char* pszCorp) {}
bool CMarketStream::GetMarketStreamByKey(const char* szCombBondKey,
                                         const char* szKey,
                                         MarketStreamInfo& stInfo,
                                         unsigned int timeDay) {
  return GetBondDeal(szCombBondKey, szKey, stInfo, 0, timeDay);
}
bool CMarketStream::GetMarketStreamByBrokers(
    const std::vector<BrokerKey>& brokerKeys,
    std::vector<MarketStreamInfo>& records, time_t t, unsigned int day) {
  return GetBondDealBatch(brokerKeys, records, t, day);
}
bool CMarketStream::GetLastMarketStream(
    const std::vector<BrokerKey>& brokerKeys,
    std::vector<MarketStreamInfo>& records, time_t t, uint32_t day) {
  return GetLastBondDeal(brokerKeys, records, t, day);
}
bool CMarketStream::GetNearByMarketStream(const char* pBondCombKey, time_t t,
                                          const char* pBrokerId,
                                          MarketStreamInfo& info,
                                          unsigned int timeDay) {
  return GetBondDeal(pBondCombKey, pBrokerId, info, t, timeDay);
}
// CMarketStreamlst CMarketStream::GetBondMarketStream(const
// std::vector<unsigned int>& timeVector)
//{
//	// 外部需要有锁
//	static CMarketStreamlst mklist;
//	mklist.m_list.clear();
//	mklist.m_Record.clear();
//	MarkStreamListArg arg;
//	arg.days = timeVector;
//	GetMarketStreamList(arg, mklist.m_list, &mklist.m_Record);
//	return mklist;
// }
// CMarketStreamlst& CMarketStream::GetBondMarketStream(unsigned int timeDay)
//{
//	// 外部需要有锁
//	static CMarketStreamlst mklist;
//	mklist.m_list.clear();
//	mklist.m_Record.clear();
//	MarkStreamListArg arg;
//	arg.days.push_back(timeDay<=0?GetBondDealInvalidateDay():timeDay);
//	GetMarketStreamList(arg, mklist.m_list, &mklist.m_Record);
//	return mklist;
// }

// 只使用到了m_list部分,且有严重的性能问题和线程安全问题
// 需要的是m_list组成以map<string,m_body.m_price>,key为m_bondkey,m_listedmarket,m_company_id
// 重构计划是提供一个返回此类数据的接口
// CMarketStreamlst* CMarketStream::GetLastDayMarketStreamByCorp(int nCorp)
//{
//	// 外部需要有锁
//	static CMarketStreamlst mklist;
//	mklist.m_list.clear();
//	mklist.m_Record.clear();
//	MarkStreamListArg arg;
//	arg.corp = nCorp;
//	arg.days.push_back(GetBondDealInvalidateDay());
//	GetMarketStreamList(arg, mklist.m_list, &mklist.m_Record);
//	return &mklist;
//}
bool CMarketStream::GetTFRelativeStream(CMarketStreamlst& Marketlist,
                                        unsigned int timeDay) {
  MarkStreamListArg arg;
  arg.days.push_back(timeDay <= 0 ? GetBondDealInvalidateDay() : timeDay);
  arg.page_type = mktPage_TFRelative;
  return GetMarketStreamList(arg, Marketlist.m_list, &Marketlist.m_Record);
}
bool CMarketStream::GetInterestRateStream(CMarketStreamlst& Marketlist,
                                          unsigned int timeDay) {
  MarkStreamListArg arg;
  arg.days.push_back(timeDay <= 0 ? GetBondDealInvalidateDay() : timeDay);
  arg.page_type = mktPage_Interest;
  return GetMarketStreamList(arg, Marketlist.m_list, &Marketlist.m_Record);
}
bool CMarketStream::GetCreditRateStream(CMarketStreamlst& Marketlist,
                                        unsigned int timeDay,
                                        bool bNeedNCD /*是否含NCD*/) {
  MarkStreamListArg arg;
  arg.days.push_back(timeDay <= 0 ? GetBondDealInvalidateDay() : timeDay);
  arg.page_type = bNeedNCD ? mktPage_Credit_Inc_NCD : mktPage_Credit;
  return GetMarketStreamList(arg, Marketlist.m_list, &Marketlist.m_Record);
}

// 经过调查以下接口中并没有使用到CMarketStreamlst.m_Record信息
bool CMarketStream::GetLastDayMSByCorp(int nCorp, CMarketStreamlst& mktLst) {
  MarkStreamListArg arg;
  arg.days.push_back(GetBondDealInvalidateDay());
  arg.corp = nCorp;
  return GetMarketStreamList(arg, mktLst.m_list, &mktLst.m_Record);
}

bool CMarketStream::GetMarketInfoByType(CMarketStreamlst& Marketlist,
                                        MarktType mkType,
                                        unsigned int timeDay) {
  MarkStreamListArg arg;
  arg.days.push_back(GetBondDealInvalidateDay());
  arg.mark_type = mkType;
  return GetMarketStreamList(arg, Marketlist.m_list, &Marketlist.m_Record);
}
bool CMarketStream::GetMarketInfoByType(
    CMarketStreamlst& Marketlist, MarktType mkType,
    const std::vector<unsigned int>& timeVector) {
  MarkStreamListArg arg;
  arg.days = timeVector;
  arg.mark_type = mkType;
  return GetMarketStreamList(arg, Marketlist.m_list, &Marketlist.m_Record);
}

bool CMarketStream::GetInterestRateStream(
    CMarketStreamlst& Marketlist, const std::vector<unsigned int>& timeVector) {
  MarkStreamListArg arg;
  arg.days = timeVector;
  arg.page_type = mktPage_Interest;
  return GetMarketStreamList(arg, Marketlist.m_list, &Marketlist.m_Record);
}
bool CMarketStream::GetCreditRateStream(
    CMarketStreamlst& Marketlist, const std::vector<unsigned int>& timeVector,
    bool bNeedNCD) {
  MarkStreamListArg arg;
  arg.days = timeVector;
  arg.page_type = bNeedNCD ? mktPage_Credit_Inc_NCD : mktPage_Credit;
  return GetMarketStreamList(arg, Marketlist.m_list, &Marketlist.m_Record);
}
bool CMarketStream::GetMarketRecord(CMarketToprec& MarketInfo,
                                    const std::vector<unsigned int>& timeVector,
                                    unsigned int nActvNum) {
  // 成交概览 -- 活跃成交
  MarketStreamTopArg arg;
  arg.count = nActvNum;
  arg.days = timeVector;
  return GetTopRecords(arg, MarketInfo);
}
bool CMarketStream::GetTodayMarketRecord(CMarketToprec& MarketInfo,
                                         const CMarketStreamlst& lst,
                                         unsigned int nActvNum) {
  return detail::MSUtil::GetTodayMarketRecord(MarketInfo, lst, nActvNum);
}
// static bool	GetTodayMarketRecord(CMarketToprec &MarketInfo, const
// CMarketStreamlst &lst, unsigned int nActvNum = 10); //获取当天活跃成交
bool CMarketStream::GetMarketActvTrans(
    CMarketToprec& MarketInfo, MarktType mkType,
    const std::vector<unsigned int>& timeVector,
    unsigned int nActvNum)  // 子窗口 -- 活跃成交
{
  MarketStreamTopArg arg;
  arg.count = nActvNum;
  arg.days = timeVector;
  arg.mark_type = mkType;
  return GetTopRecords(arg, MarketInfo);
}

bool CMarketStream::GetMarketInfoByType(
    CMarketInfoMap& MarketInfo,
    const std::vector<unsigned int>&
        timeVector) {  // 成交概览 -- 成交分布--
                       // 按新QB产品类型分类，即地方债、国债、央票、金融债、短融、中票、企业债、其他
  MarketStreamInfoArg arg;
  arg.days = timeVector;
  arg.stat_type = kStatTypeByType;
  return GetMarketInfo(arg, MarketInfo);
}

bool CMarketStream::GetMarketInfoByType(CMarketInfoMap& MarketInfo,
                                        unsigned int timeDay) {
  MarketStreamInfoArg arg;
  arg.days.push_back(timeDay <= 0 ? GetBondDealInvalidateDay() : timeDay);
  arg.stat_type = kStatTypeByType;
  return GetMarketInfo(arg, MarketInfo);
}
bool CMarketStream::GetMarketInfoByDate(
    CMarketInfoMap& MarketInfo, const std::vector<unsigned int>& timeVector) {
  MarketStreamInfoArg arg;
  arg.days = timeVector;
  arg.stat_type = kStatTypeByDate;
  return GetMarketInfo(arg, MarketInfo);
}
bool CMarketStream::GetMarketInfoByDate(CMarketInfoMap& MarketInfo,
                                        unsigned int timeDay) {
  MarketStreamInfoArg arg;
  arg.days.push_back(timeDay <= 0 ? GetBondDealInvalidateDay() : timeDay);
  arg.stat_type = kStatTypeByDate;
  return GetMarketInfo(arg, MarketInfo);
}

bool CMarketStream::GetMarketTransDate(
    CMarketInfoMap& MarketInfo, MarktType mkType,
    const std::vector<unsigned int>& timeVector)  // 国债 -- 成交分布
{
  MarketStreamInfoArg arg;
  arg.days = timeVector;
  arg.stat_type = kStatTypeTransDate;
  arg.mark_type = mkType;
  return GetMarketInfo(arg, MarketInfo);
}
bool CMarketStream::GetMarketTransDate(CMarketInfoMap& MarketInfo,
                                       MarktType mkType, unsigned int timeDay) {
  MarketStreamInfoArg arg;
  arg.days.push_back(timeDay <= 0 ? GetBondDealInvalidateDay() : timeDay);
  arg.stat_type = kStatTypeTransDate;
  arg.mark_type = mkType;
  return GetMarketInfo(arg, MarketInfo);
}

bool CMarketStream::GetMarketRatioByType(
    CMarketInfoMap& MarketInfo, MarktType mkType,
    const std::vector<unsigned int>& timeVector) {
  MarketStreamInfoArg arg;
  arg.days = timeVector;
  arg.stat_type = kStatTypeRatioType;
  arg.mark_type = mkType;
  return GetMarketInfo(arg, MarketInfo);
}
bool CMarketStream::GetMarketRatioByType(CMarketInfoMap& MarketInfo,
                                         MarktType mkType,
                                         unsigned int timeDay) {
  MarketStreamInfoArg arg;
  arg.days.push_back(timeDay <= 0 ? GetBondDealInvalidateDay() : timeDay);
  arg.stat_type = kStatTypeRatioType;
  arg.mark_type = mkType;
  return GetMarketInfo(arg, MarketInfo);
}

bool CMarketStream::GetMarketTransRating(
    CMarketInfoMap& MarketInfo, MarktType mkType,
    const std::vector<unsigned int>& timeVector) {
  MarketStreamInfoArg arg;
  arg.days = timeVector;
  arg.stat_type = kStatTypeTransRating;
  arg.mark_type = mkType;
  return GetMarketInfo(arg, MarketInfo);
}
bool CMarketStream::GetMarketTransRating(CMarketInfoMap& MarketInfo,
                                         MarktType mkType,
                                         unsigned int timeDay) {
  MarketStreamInfoArg arg;
  arg.days.push_back(timeDay <= 0 ? GetBondDealInvalidateDay() : timeDay);
  arg.stat_type = kStatTypeTransRating;
  arg.mark_type = mkType;
  return GetMarketInfo(arg, MarketInfo);
}

bool CMarketStream::GetMarketTransNFRate(
    CMarketInfoMap& MarketInfo, MarktType mkType,
    const std::vector<unsigned int>& timeVector) {
  MarketStreamInfoArg arg;
  arg.days = timeVector;
  arg.stat_type = kStatTypeTransNFRate;
  arg.mark_type = mkType;
  return GetMarketInfo(arg, MarketInfo);
}

bool CMarketStream::GetMarketTransNFRate(CMarketInfoMap& MarketInfo,
                                         MarktType mkType,
                                         unsigned int timeDay) {
  MarketStreamInfoArg arg;
  arg.days.push_back(timeDay <= 0 ? GetBondDealInvalidateDay() : timeDay);
  arg.stat_type = kStatTypeTransNFRate;
  arg.mark_type = mkType;
  return GetMarketInfo(arg, MarketInfo);
}